#include "logindialog.h"
#include "selection.h"
#include "ui_logindialog.h"
#include "regesterwindow.h"
#include "qmessagebox.h"
#include "administrator.h"
#include "user.h"
#include "user_io.h"

LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
}

LoginDialog::~LoginDialog()
{
    delete ui;
}

std::string username;
std::string pwd_2;

void LoginDialog::on_loginBtn_clicked()  //密码校验代码
{
    loadUsersFromFile("D:\\QT_projects\\Login\\1.txt");
    for(const User&user :load_users){
        QString qstr = ui->usrLineEdit->text();
        QString pwd = ui->pwdLineEdit->text();
        username = qstr.toStdString();
        pwd_2 = pwd.toStdString();
        if(username == user.username && pwd_2 == user.password){
            Selection *select_time = new Selection(this);
            select_time->show();
            select_time->setWindowTitle("场次选择");

            QLabel label;
            QPixmap pixmap("D:\\QT_projects\\Login\\123.png");

            label.setPixmap(pixmap);
            // 调整 QLabel 控件的大小以适应图片
            label.resize(pixmap.width(), pixmap.height());
            // 显示窗口
            label.show();

            //this->destroy();
            //this->destroy();
            return;
        }
    }


    if (ui->usrLineEdit->text() == "0" && //管理员登录
                   ui->pwdLineEdit->text() == "0") {
        Administrator *admist = new Administrator(this);
        admist->show();
        admist->setWindowTitle("用户注册");
        //this->destroy();
         }
        else {
           QMessageBox::warning(this, tr("警告！"),
                       tr("用户名或密码错误！"),
                       QMessageBox::Yes);
        }

}

void LoginDialog::on_pushButton_clicked()
{
    RegesterWindow *regesterdialog = new RegesterWindow(this);
    regesterdialog->show();
}
